﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySkiPatrolEntity;
using System.Data;
using MySkiPatrolSQL;

namespace MySkiPatrolDAL
{
    public class ArticleDAO
    {
        SqlConnector SQLCONN = new SqlConnector();

        public ArticleList GetArticles(ArticleRequest data)
        {
            //setup the return object
            ArticleList myReturn = new ArticleList();
            myReturn.Articles = new List<Article>();
            if (myReturn.DataTransaction == null) myReturn.DataTransaction = new DataTransaction();
            if (myReturn.DataTransaction.TransactionErrors == null) myReturn.DataTransaction.TransactionErrors = new List<Error>();
            if (myReturn.DataTransaction.TransactionLog == null) myReturn.DataTransaction.TransactionLog = new List<LogEntry>();
            myReturn.DataTransaction.Success = true;
            myReturn.DataTransaction.Action = DataAction.View;

            try
            {
                DataSet ds = SQLCONN.RunSQL_DataSet(ArticleSQL.GetArticleList(data), "data");
                DataTable dt = ds.Tables["data"];

                foreach (DataRow dr in dt.Rows)
                {
                    Article item = new Article();

                    item.Body = DataConvertor.toString(dr, "Body");
                    item.ID = DataConvertor.toString(dr, "ArticleID");
                    item.Title = DataConvertor.toString(dr, "Title");
                    item.EffectiveDate = DataConvertor.toDateTime(dr, "EffectiveDate");
                    item.ExpiryDate = DataConvertor.toDateTime(dr, "ExpirationDate");
                    item.IsPublic = DataConvertor.toBool(dr, "IsPublic");

                    myReturn.Articles.Add(item);
                }
            }
            catch (System.Exception ex)
            {
                myReturn.DataTransaction.Success = false;
                myReturn.DataTransaction.TransactionErrors.Add(new Error("DAO10001", "An error occurred collecting the list of articles/r" + ex.Message));
            }

            return myReturn;
        }

        public Article GetArticle(Article data)
        {
            //setup the return object
            if (data.DataTransaction == null) data.DataTransaction = new DataTransaction();
            if (data.DataTransaction.TransactionErrors == null) data.DataTransaction.TransactionErrors = new List<Error>();
            if (data.DataTransaction.TransactionLog == null) data.DataTransaction.TransactionLog = new List<LogEntry>();
            data.DataTransaction.Success = true;
            data.DataTransaction.Action = DataAction.View;

            try
            {
                DataSet ds = SQLCONN.RunSQL_DataSet(ArticleSQL.GetArticle(data), "data");
                DataTable dt = ds.Tables["data"];

                foreach (DataRow dr in dt.Rows)
                {
                    data.Body = DataConvertor.toString(dr, "Body");
                    data.ID = DataConvertor.toString(dr, "ArticleID");
                    data.Title = DataConvertor.toString(dr, "Title");
                    data.EffectiveDate = DataConvertor.toDateTime(dr, "EffectiveDate");
                    data.ExpiryDate = DataConvertor.toDateTime(dr, "ExpirationDate");
                    data.IsPublic = DataConvertor.toBool(dr, "IsPublic");
                }
            }
            catch (System.Exception ex)
            {
                data.DataTransaction.Success = false;
                data.DataTransaction.TransactionErrors.Add(new Error("DAO10001", "An error occurred fetching the article from the database./r" + ex.Message));
            }

            return data;
        }

        public Article AddArticle(Article data)
        {
            try
            {
                DataSet ds = SQLCONN.RunSQL_DataSet(ArticleSQL.AddArticle(data), "data");
                DataTable dt = ds.Tables["data"];

                data.DataTransaction.Success = true;
            }
            catch (Exception ex)
            {
                data.DataTransaction.Success = false;
                data.DataTransaction.TransactionErrors.Add(new Error("DAO10001", "An error occurred creating the article./r" + ex.Message));
            }

            return data;
        }

        public Article UpdateArticle(Article data)
        {
            try
            {
                DataSet ds = SQLCONN.RunSQL_DataSet(ArticleSQL.UpdateArticle(data), "data");
                DataTable dt = ds.Tables["data"];

                data.DataTransaction.Success = true;
            }
            catch (Exception ex)
            {
                data.DataTransaction.Success = false;
                data.DataTransaction.TransactionErrors.Add(new Error("DAO10001", "An error occurred updating the article./r" + ex.Message));
            }

            return data;
        }

        public Article DeleteArticle(Article data)
        {
            try
            {
                DataSet ds = SQLCONN.RunSQL_DataSet(ArticleSQL.DeleteArticle(data), "data");
                DataTable dt = ds.Tables["data"];

                data.DataTransaction.Success = true;
            }
            catch (Exception ex)
            {
                data.DataTransaction.Success = false;
                data.DataTransaction.TransactionErrors.Add(new Error("DAO10001", "An error occurred updating the article./r" + ex.Message));
            }

            return data;
        }
    }
}
